package com.wframe.project.jnd.dao;

import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.frame.database.JdataBean;
import com.frame.util.DateConvert;
import com.wframe.framework.database.BaseDao;
import com.wframe.framework.database.ParmSqlVo;
import com.wframe.framework.util.ConversionUtil;

@Repository
public class UserDao extends BaseDao {

    public JdataBean wblogin(String loginname) {
        return getObject("select * from wbuser where (loginname=? or loginphone=?) and state<>-1", new Object[]{loginname, loginname});
    }

    public void updateLogintime(String userid) {
        execute("update wbuser set logintime =now() where id=?", new Object[]{userid});
    }

    public JdataBean getWebUser(JdataBean parms) {
        String sql = "select * from wbuser u where 1=1";
        List patmlist = new ArrayList();
        if (parms != null) {
            ParmSqlVo vo = ConversionUtil.beanToSql(parms);
            sql += " and " + vo.getInSql();
            patmlist = vo.getParms();
        }
        return getObject2(sql, patmlist);
    }

    public JdataBean getUserByLoginName(String name) {
        return getObject("select * from wbuser where loginname=? and state<>-1", new Object[]{name});
    }

    public JdataBean getUserByLoginPhone(String phone) {
        return getObject("select * from wbuser where loginphone=? and state<>-1", new Object[]{phone});
    }

    public List<JdataBean> getUserByPhone(String phone) {
        return querySql("select * from wbuser where phone=?", new Object[]{phone});
    }

    public JdataBean getUser(String id) {
        return getObject("select * from wbuser where id=?", new Object[]{id});
    }
    /*点击签到*/
    public void saveSignRecord(JdataBean bean) throws Exception {
         save("wb_sign",bean.getDataMap());
    }
    public JdataBean getquerywbsign(String userid){
        return getObject("SELECT * from wb_sign WHERE date_format(create_time,'%Y-%m-%d')=date_format(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d') and userid=? limit 1",new Object[]{userid});
    }
//    public void updateIntegral(String userid,int integral){
//        execute("update wbuser set integral = integral+? where id=?", new Object[]{integral,userid});
//    }
    public JdataBean queryToday(String userid){
        return getObject("select * from wb_sign where to_days(create_time) = to_days(now()) and userid =?",new Object[]{userid});
    }


    public JdataBean querysigncount(String userid){
       return getObject("SELECT * FROM wb_sign WHERE TO_DAYS(NOW( ) ) - TO_DAYS( create_time) = 1 and userid=?",new Object[]{userid});
    }

    public JdataBean getquerywbuser(String id){
        return getObject("SELECT wb.photo,wb.nickname,wc.name AS wcname FROM wbuser wb,wb_company wc WHERE wb.ID = wc.userid AND wb.ID =?" ,new Object[]{id});
    }

    public String addUser(JdataBean user) throws Exception {
        user.put("regtime", new Date());
        return saveReturnKey("wbuser", user.getDataMap());
    }

    public void editUser(JdataBean bean) throws Exception {
        Map k = new HashMap();
        k.put("id", bean.getString("id"));
        update("wbuser", bean.getDataMap(), k);
    }

    public JdataBean getUserCompany(String userid) {
        return getObject("select * from wb_company where userid=?", new Object[]{userid});
    }

    public String saveCompany(JdataBean bean) throws Exception {
        if (StringUtils.isNotBlank(bean.getString("id"))) {
            Map k = new HashMap();
            k.put("id", bean.getString("id"));
            update("wb_company", bean.getDataMap(), k);
            execute("update wb_company set authtime=null,authcon=null where id=?", new Object[]{bean.getString("id")});
            return bean.getString("id");
        } else {
            return saveReturnKey("wb_company", bean.getDataMap());
        }
    }

    public void delCompanyQul(String comid) {
        execute("delete from wb_company_qul where companyid=?", new Object[]{comid});
    }

    public String saveCompanyQul(JdataBean bean) throws Exception {
        if (StringUtils.isNotBlank(bean.getString("id"))) {
            Map k = new HashMap();
            k.put("id", bean.getString("id"));
            update("wb_company_qul", bean.getDataMap(), k);
            return bean.getString("id");
        } else {
            return saveReturnKey("wb_company_qul", bean.getDataMap());
        }
    }

    public List<Map> queryCompanyQul(String comid) {
        return querySqlMap("select * from wb_company_qul where companyid=? order by id", new Object[]{comid});
    }

    public void addBrowse(String userid, int type, int dataid) throws Exception {
        Map data = new HashMap();
        data.put("type", type);
        data.put("dataid", dataid);
        Map key = new HashMap();
        key.put("userid", userid);
        int n = update("wb_browse", data, key);
        data.put("userid", userid);
        data.put("createtime", new Date().getTime() / 1000);
        if (n == 0) save("wb_browse", data);
    }

    public JdataBean getBrowseLatest(String userid) {
        return getObject("select * from wb_browse where userid=? order by createtime desc limit 0,1", new Object[]{userid});
    }

    public int statUsercoupon(String userid, int source) {
        JdataBean data = getObject("select count(id) num from wb_coupon where userid=? and (source=? or source is null or source=0) and state=1 and tag=1 and (enddate is null or enddate>='" + DateConvert.toString(new Date()) + "')", new Object[]{userid, source});
        if (data != null) return data.getInt("num");
        return 0;
    }

    public int statUsercart(String userid) {
        JdataBean data = getObject("select count(id) num from wb_shopcart where userid=?", new Object[]{userid});
        if (data != null) return data.getInt("num");
        return 0;
    }

    public JdataBean getUserByAccount(String account) {
        return getObject("select * from  wbuser where loginname=? and state<>-1", new Object[]{account});
    }

    public void writefeedback(JdataBean bean) throws Exception {
        save("wb_feedback", bean.getDataMap());
    }

    public void PCFeedback(JdataBean bean) throws Exception{
        save("wb_feedback",bean.getDataMap());
    }

    public List<JdataBean> getUserByWechat(String openid, String unionid) {

        if (StringUtils.isEmpty(openid) && StringUtils.isEmpty(unionid)) {
            return new ArrayList<JdataBean>();
        }

        String sql = "select * from wbuser where ";
        String para1 = "";
//        String para2 = "";
        List<Object> paras = new ArrayList<Object>();

        if (StringUtils.isNotEmpty(openid)) {
            para1 = " wechat_openid=? ";
            paras.add(openid.trim());
        }
//        if (StringUtils.isNotEmpty(unionid)) {
//            para1 = " wechat_unionid=? ";
//            paras.add(unionid.trim());
//        }

//        if (StringUtils.isNotEmpty(para1) && StringUtils.isNotEmpty(para2)) {
//            sql = sql + para1 + " or " + para2;
//        } else {
//            sql = sql + para1 + para2;
//        }

        if(StringUtils.isNotEmpty(para1)){
            sql = sql + para1;
        }else{
            return null;
        }

        return querySql(sql, paras.toArray());
    }

    public boolean bindUserWechat(Integer userid,String openid,String unionid){
        if(userid==null||openid==null){
            return false;
        }
        execute("update wbuser set wechat_openid = null where wechat_openid = ?",new Object[]{ StringUtils.trimToEmpty(openid) });
        execute("update wbuser set wechat_openid = ? where id = ? ",new Object[]{ StringUtils.trimToEmpty(openid) , userid });
        return true;
    }
    public void  wxLoginOutwe(String wechat_openid,String userid) {
        execute("update wbuser set wechat_openid = ? where id=?",new Object[]{wechat_openid , userid});
    }
}
